package GUILayer.TableModel;

import java.util.ArrayList;

import javax.swing.table.AbstractTableModel;

import DataLayer.Model.DelOrdre;
import DataLayer.Model.EkstraService;
import DataLayer.Model.Service;

@SuppressWarnings("serial")
public class PaintingTableModel extends AbstractTableModel
{
	private ArrayList<DelOrdre> li = new ArrayList<DelOrdre>();
	private String[] columnNames = { "ID", "Spil", "Model",
			"Standard", "Antal", "Pris" };

	public PaintingTableModel()
	{
	}

	public void PopulateList(ArrayList<DelOrdre> list)
	{
		if (list != null)
		{
			this.li.clear();
			for (DelOrdre dO : list)
			{
				this.li.add(dO);
				if (dO.getEkstraServiceList() != null)
				{
					for (EkstraService es : dO.getEkstraServiceList())
					{
						Service s = new Service("", "", es.getBeskrivelse(), es.getPris(), true);
						DelOrdre d = new DelOrdre();
						d.setAntal(dO.getAntal());
						d.setDelordreId(dO.getDelordreId());
						d.setService(s);
						this.li.add(d);
					}
				}
			}
		}
	}

	public void fireTableDataChanged()
	{
		fireTableDataChanged();
	}

	@Override
	public String getColumnName(int columnIndex)
	{
		return columnNames[columnIndex];
	}

	@Override
	public int getRowCount()
	{
		return li.size();
	}

	@Override
	public int getColumnCount()
	{
		return 6;
	}

	@Override
	public Object getValueAt(int rowIndex, int columnIndex)
	{
		if (li.size() > rowIndex)
		{
			DelOrdre dO = li.get(rowIndex);
			switch (columnIndex)
			{
			case 0:
				return dO.getDelordreId();
			case 1:
				return dO.getService().getSpil();
			case 2:
				return dO.getService().getModel();
			case 3:
				return dO.getService().getMalestandard();
			case 4:
				return dO.getAntal();
			case 5:
				return dO.getService().getPris() * dO.getAntal();
			}
		}
		return null;
	}

	@Override
	public Class<?> getColumnClass(int columnIndex)
	{
		switch (columnIndex)
		{
		case 0:
			return int.class;
		case 1:
			return String.class;
		case 2:
			return String.class;
		case 3:
			return String.class;
		case 4:
			return int.class;
		case 5:
			return (li.isEmpty()) ? double.class : getValueAt(0, columnIndex).getClass();
		}
		return null;
	}
}